<?php

namespace Biz\model\activity;

use Biz\service\Goods;
use Doraemon\service\Member;
use Doraemon\tools\ImgUrl;
use service\User\User;


class PageModuleGoods extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'page_module_goods';


    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;


    public static function insertModuleGoods($data)
    {
        $data['status'] = self::STATUS_NORMAL;
        $data['addtime'] = date("Y-m-d H:i:s");
        if (self::insert($data)->rowcount()) {
            return self::id();
        } else {
            return false;
        }
    }

    public static function addModuleGoods($pageId, $moduleId, $goodsIds)
    {
        foreach ($goodsIds as $goodsId) {
            $data = [
                'page_id' => $pageId,
                'module_id' => $moduleId,
                'goods_id' => $goodsId,
                'status' => self::STATUS_NORMAL,
                'addtime' => date('Y-m-d H:i:s')
            ];
            self::insert($data);
        }
        return true;
    }

    public static function updateModuleGoods($goodsId, $data)
    {
        $ret = self::update($data, ['id' => $goodsId]);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function saveModuleGoodsSort($sortInfo)
    {
        foreach ($sortInfo as $goodsId => $sort) {
            self::update(['sort' => $sort], ['id' => $goodsId]);
        }
    }

    public static function getSpecialGoodsByModuleId($moduleId, $format = true)
    {
        $moduleGoods = self::select("*",
            [
                'AND' => [
                    'module_id' => $moduleId,
                    'status' => self::STATUS_NORMAL
                ],
                'ORDER' => [
                    'sort',
                    'id'
                ]
            ]);

        if ($format) {
            $goodsIds = array_column($moduleGoods, 'goods_id');
            $uids = array_column($moduleGoods, 'uid');
            $userInfo = \Doraemon\service\User::getusers_baisic_info($uids);
            $goodsInfoMap = Goods::Goods($goodsIds, Goods::FIELDS_GROUP_DETAIL);
            foreach ($moduleGoods as &$goods) {
                $goods['goods_info'] = $goodsInfoMap[$goods['goods_id']] ?? [];
                $goods['user_info'] = $userInfo[$goods['uid']] ?? [];
                $goods['discount_icon_url'] = empty($goods['discount_icon']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $goods['discount_icon'], ImgUrl::IMAGE_SIZE_OP_ORI);
            }
        }

        return $moduleGoods;
    }

    public static function getGoodsByModuleId($moduleId, $format = true)
    {
        $moduleGoods = self::select("*",
            [
                'AND' => [
                    'module_id' => $moduleId,
                    'status' => self::STATUS_NORMAL
                ],
                'ORDER' => [
                    'sort',
                    'id'
                ]
            ]);
        return $format ? self::formatGoods($moduleGoods) : $moduleGoods;
    }

    private static function formatGoods($moduleGoods)
    {
        $goodsIds = array_column($moduleGoods, 'goods_id');
        $goodsInfos = Goods::Goods($goodsIds);
        foreach ($moduleGoods as &$goods) {
            if (isset($goodsInfos[$goods['goods_id']])) {
                $goods['goods_info'] = $goodsInfos[$goods['goods_id']];
            }
        }
        return $moduleGoods;
    }

    public static function chageGoodsStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }
}